package com.example.controller;

import com.example.domain.SysLog;
import com.example.service.ISysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @Date: 2021年08月22日 13:47
 * @Author: LHSAMA
 */

@Component
@Aspect
public class LogAop {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ISysLogService sysLogService;

    private Date visitTime;
    private Class clazz;
    private Method method;
    private Long time;
    private String url;
    private String ip;
    private String userName;

    @Before("execution(* com.example.controller.*.*(..))")
    public void doBefore(JoinPoint jp) throws NoSuchMethodException {
        visitTime = new Date();
        clazz = jp.getTarget().getClass();
        Object[] argsStr = jp.getArgs();
        String methodName = jp.getSignature().getName();
    if(argsStr == null|| argsStr.length == 0){
        method = clazz.getMethod(methodName);
    }else{
        Class[] arges = new Class[argsStr.length];
        for(int i = 0;i < argsStr.length;i++){
            arges[i]=argsStr[i].getClass();
        }
        method = clazz.getMethod(methodName,arges);
    }

    }

    @After("execution(* com.example.controller.*.*(..))")
    public void doAfter(JoinPoint jp) throws Exception {
        time = new Date().getTime()-visitTime.getTime();
        url = "";
        if(clazz!=null&&method!=null&& clazz!=LogAop.class){
            RequestMapping clazzAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);

            if(clazzAnnotation!=null){
                String[] clazzValue = clazzAnnotation.value();
                RequestMapping methodAnnotation = (RequestMapping) method.getAnnotation(RequestMapping.class);

                if (methodAnnotation!= null){
                    String[] methodValue = methodAnnotation.value();

                    url = clazzValue[0]+methodValue[0];

                    ip = request.getRemoteAddr();

                    SecurityContext context = SecurityContextHolder.getContext();
                    User user = (User)context.getAuthentication().getPrincipal();
                    userName = user.getUsername();

                    SysLog sysLog = new SysLog();
                    sysLog.setExecutionTime(time);
                    sysLog.setIp(ip);
                    sysLog.setMethod("[类名] "+clazz.getName()+"  [方法] "+method.getName());
                    sysLog.setUrl(url);
                    sysLog.setUsername(userName);
                    sysLog.setVisitTime(visitTime);

                    sysLogService.save(sysLog);
                }
            }


        }

    }


}
